<template>
  <view style="width: 100%;height: 100%;">
    <scroll-view v-show="show" class="content" scroll-y :lower-threshold="200" @scrolltolower="end"
                 refresher-enabled="true" :refresher-triggered="triggered"
				 @scroll="onscroll" :scroll-top="scrollTop"
                 :refresher-threshold="100" refresher-background="#fff"
                 @refresherrefresh="onRefresh">
      <!--    class-->
      <view class="class-box">
        <view class="class-title">
          <view class="title-box">
            <view class="class-icon">
              <image mode="aspectFill" :src="image_hosts+'q_icon_fenlie@2x.png'"></image>
            </view>
            <view class="class-msg">
              <view class="text1">圈子分类</view>
              <view class="class-msg-xian"></view>
            </view>
          </view>
          <view class="msg" @click="toRole">
            奖励规则
          </view>
        </view>
        <view class="class-list">
          <view :class="classList.length>3?'class-item-num4':''" class="class-item" v-for="(item,index) in classList"
                @click="toClass(item.id,index)">
            <view class="image">
              <image mode="aspectFill" :src="item.logo"></image>
            </view>
            <view class="title">{{ item.categoryName }}</view>
          </view>
        </view>
      </view>
      <!--    今日话题-->
      <view class="item" v-if="todayTopic.circleCommentFroneResponse || todayTopic.circleTopicData">
        <view class="day-top" @click="toLog">
          <image mode="aspectFill" :src="image_hosts+'q_bg_jinrihuati@2x.png'"></image>
          <view class="toDay">往期互动
            <uni-icons size="10" type="forward" color="#fff"></uni-icons>
          </view>
        </view>
        <view class="day-content" @click="toDetails(todayTopic.circleTopicData.id)">
          <view class="topic-box">
            <view class="topic-top">
              <view class="image">
                <image mode="aspectFill" :src="todayTopic.circleTopicData.thumb"></image>
              </view>
              <view class="topic-info">
                <view class="title">{{ todayTopic.circleTopicData.title }}</view>
                <!--                              <view class="tag" :class="todayTopic.circleTopicData.cateNames.length>10?'tagAfter':''"-->
                <!--                                    v-if="todayTopic.circleTopicData.cateNames">-->
                <!--                                <text v-for="item in (todayTopic.circleTopicData.cateNames.split('、'))">{{ item }}</text>-->
                <!--                              </view>-->
                <!--                <view class="userCount">{{ todayTopic.circleTopicData.joinNum || 0 }}参与</view>-->
              </view>
            </view>
            <view class="source-box">
              <view class="source">来自：{{ todayTopic.circleTopicData.parentCateNames }}</view>
              <view class="button">{{
                  [0, 1].includes(todayTopic.circleTopicData.contentType) ? '进入话题' : '我要表个态'
                }}
              </view>
            </view>
            <view class="desc">{{ todayTopic.circleTopicData.introduction }}</view>
            <view class="user-comment-box" v-if="todayTopic.circleCommentFroneResponse">
              <view class="icon-box">
                <image mode="aspectFit" :src="image_hosts+'icon_reping@2x.png'"></image>
              </view>
              <view class="user-info">
                <view class="user-image">
                  <image mode="aspectFit" :src="todayTopic.circleCommentFroneResponse.avatar"></image>
                </view>
                <view class="user-comment">
                  <view class="username goBeyond">
                    <view class="name">{{ todayTopic.circleCommentFroneResponse.nickName }}</view>
                    <daren v-if="todayTopic.circleCommentFroneResponse.commentLevel>0" :type="1"
                           :index="parseInt(todayTopic.circleCommentFroneResponse.commentLevel)"></daren>
                    <daren v-if="todayTopic.circleCommentFroneResponse.shineLevel>0" :type="2"
                           :index="parseInt(todayTopic.circleCommentFroneResponse.shineLevel)"></daren>
                  </view>
                  <view class="comment goBeyond">{{ todayTopic.circleCommentFroneResponse.content }}</view>
                </view>
              </view>
            </view>
            <view class="buttons">
              <view class="button-item button-item-active"
                    @click.stop="setTopicLike($event,todayTopic.circleTopicData.id,todayTopic.circleTopicData.praised,'item')">
                <image
                    :src="image_hosts+(todayTopic.circleTopicData.praised===1?'dianzan1.png':'dianzan2.png')"
                    mode="aspectFit"></image>
                <text> {{ $toNum(todayTopic.circleTopicData.praiseNum) }}支持</text>
              </view>
              <view class="button-item" @click.stop="toDetails(todayTopic.circleTopicData.id)">
                <image :src="image_hosts+'q_icon_pinglun@2x.png'" mode="aspectFill"></image>
                <text>{{ $toNum(todayTopic.circleTopicData.commentNum) }}评论</text>
              </view>
              <button open-type="share" class="button-item" @click.stop="setShare(todayTopic.circleTopicData)">
                <image :src="image_hosts+'q_icon_fenxiang@2x(1).png'" mode="aspectFill"></image>
                <text>分享{{ $toNum(todayTopic.circleTopicData.forwarding) }}</text>
              </button>
            </view>
          </view>
        </view>
      </view>
      <!--    热门推荐-->
      <view class="class-box top-list">
        <view class="class-title">
          <view class="title-box">
            <view class="class-icon">
              <image mode="aspectFit" :src="image_hosts+'q_icon_tuijan@2x.png'"></image>
            </view>
            <view class="class-msg">
              <view class="text1">热门推荐</view>
              <view class="class-msg-xian"></view>
            </view>
          </view>
        </view>
        <view class="list">
          <view class="topic-item" v-for="(item,index) in recommentTopicList"
                @click="toDetails(item.id,index,item.tag)">
            <view class="topic-box">
              <view class="topic-top">
                <view class="image">
                  <image :src="item.thumb" mode="aspectFill"></image>
                  <view class="image-icon" v-if="index===0">热门</view>
                  <view class="image-icon tuijian" v-if="index>0 && index<3">推荐</view>
                </view>
                <view class="topic-info">
                  <view class="title">{{ item.title }}</view>
                  <view class="tag-box">
                    <view class="source">来自：{{ item.parentCateNames }}</view>
                    <!--                    <view class="tag" :class="item.cateNames.length>10?'tagAfter':''" v-if="item.cateNames">-->
                    <!--                      <text v-for="text in item.cateNames.split('、')">{{ text }}</text>-->
                    <!--                    </view>-->
                    <view class="button" v-if="item.followed===0" @click.stop="setTopicFollow($event,item,index)">
                      <uni-icons color="#00C141" type="plusempty"></uni-icons>
                      关注
                    </view>
                    <view class="button yi-button" v-if="item.followed===1"
                          @click.stop="setTopicFollow($event,item,index)">
                      已关注
                    </view>
                  </view>

                  <!--                  <view class="userCount">{{ item.joinNum || 0 }}参与</view>-->
                </view>
              </view>
              <view class="desc">
                <!--                                文案-->
                <text v-if="[0,1].includes(item.contentType)">{{ item.introduction }}</text>
                <!--                                投票-->
                <vote :item="item" :ref="'vote'" v-show="item.contentType===3"></vote>
                <!--                pk-->
                <pk :item="item" :ref="'pk'" v-show="item.contentType===2"></pk>
              </view>
              <view class="images" v-if="JSON.parse(item.picList)">
                <view class="image-item" v-for="(fileItem,index) in JSON.parse(item.picList)"
                      @click="showImages(index,JSON.parse(item.picList))">
                  <image mode="aspectFill" v-if="fileItem.fileType==='image'" class="image" :src="fileItem.url"></image>
                  <image mode="aspectFill" v-if="fileItem.fileType==='video'" class="image"
                         :src="fileItem.thumbUrl"></image>
                  <view class="video-play" v-if="fileItem.fileType==='video'">
                    <image :src="image_hosts+'icon_play.png'"></image>
                  </view>
                </view>
              </view>
              <view class="buttons">
                <view class="button-item button-item-active"
                      @click.stop="setTopicLike($event,item.id,item.praised,'list',index)">
                  <image class="button-item-image" :src="image_hosts+(item.praised===1?'dianzan1.png':'dianzan2.png')"
                         mode="aspectFill"></image>
                  <text>{{ $toNum(item.praiseNum) }}支持</text>
                </view>
                <view class="button-item button-item-active" @click.stop="toDetails(item.id)">
                  <image :src="image_hosts+'q_icon_pinglun@2x.png'" mode="aspectFill"></image>
                  <text>{{ $toNum(item.commentNum) }}评论</text>
                </view>
                <button open-type="share" class="button-item" @click.stop="setShare(item,index)">
                  <image :src="image_hosts+'q_icon_fenxiang@2x(1).png'" mode="aspectFill"></image>
                  <text>分享{{ $toNum(item.forwarding) }}</text>
                </button>
              </view>
            </view>
          </view>
        </view>
      </view>
      <to-message ref="toMessage"></to-message>
    </scroll-view>
	<back-top v-show="showBackTop" @click="backTop"></back-top>
  </view>
</template>

<script>
import toMessage from "@/pages/circle/components/toMessage.vue";
import daren from "@/pages/circle/components/daren.vue";
import vote from "@/pages/circle/components/vote.vue";
import pk from "@/pages/circle/components/pk.vue";
import BackTop from "@/components/back-top/back-top.vue"

export default {
  components: {toMessage, daren, vote, pk, BackTop},
  data() {
    return {
      show: false,
      image_hosts: this.image_host + 'quanzi/',
      scrollLeft: 0,
      messageList: [],
      classList: [],
      todayTopic: {
        circleCommentFroneResponse: "",
        circleTopicData: ""
      },
      recommentTopicPostData: {
        page: 1,
        pageSize: 10
      },
      recommentTopicTotal: 0,
      recommentTopicList: [],
      messageItemStyle: "",
      time: {},
      messageShow: true,
      triggered: false,
	  systemInfo: uni.getSystemInfoSync(),
	  showBackTop: false,
	  scrollTop: ""
    };
  },
  created() {
    console.log('show')
    this.getCircleCategory()
    this.getRecommentTopic()
  },
  methods: {

    init(type = 0) {
      this.getTodayTopic()
      if (type === 'login') {
        this.getCircleCategory()
        this.getRecommentTopic()
      }
    },
    // 刷新列表数据
    setListItem(index, info) {
      this.recommentTopicList[index].praiseNum = info.praiseNum
      this.recommentTopicList[index].praised = info.praised
      this.recommentTopicList[index].followed = info.followed
    },
    // 获取圈子分类
    async getCircleCategory() {
      let {data} = await this.$javaCircle.doRequest("post", "circle/customer/topic/getCircleCategory", "", "", false)
      this.classList = data.data
    },
    // 获取今日话题
    async getTodayTopic() {
      let {data} = await this.$javaCircle.doRequest("post", 'circle/customer/topic/getTodayTopic', '', '', false)
      this.todayTopic = data.data
      if (!data.data.circleTopicData) {
        return
      }
      this.todayTopic.circleTopicData.cateNames = this.todayTopic.circleTopicData.cateNames.substring(0, 12)
    },
    // 获取热门话题推荐
    async getRecommentTopic() {
      let postData = this.recommentTopicPostData
      let {data} = await this.$javaCircle.doRequest("post", 'circle/customer/topic/getRecommentTopic', postData, 'json', false)
      this.recommentTopicTotal = data.data.total || 0;
      let recommentTopicList = postData.page === 1 ? [] : this.recommentTopicList
      this.recommentTopicList = recommentTopicList.concat(data.data.list.map(item => {
        item.cateNames = item.cateNames.substring(0, 14)
        return item
      }))
      this.$forceUpdate()
      this.show = true
      this.triggered = false;
    },
    setPk(data) {
      let item = this.recommentTopicList[data.index];
      if (item.contentType === 2) {
        this.$refs.pk[parseInt(data.index)].setIndex(data.pkVoteIndex)
      } else if (item.contentType === 3) {
        this.$refs.vote[parseInt(data.index)].setIndex(data.pkVoteIndex)
      }

    },
    // 点赞话题
    async setTopicLike(e, topicId, liked, type, index = 0) {
      liked = liked === 0 ? 1 : 0
      let {data} = await this.$javaCircle.doRequest("post", `circle/customer/topic/setTopicLike?topicId=${topicId}&liked=${liked}`, {}, 'json', false)
      if (data.code === 0) {
        if (type === 'item') {
          this.todayTopic.circleTopicData.praised = liked
          this.todayTopic.circleTopicData.praiseNum += liked === 1 ? 1 : -1
        } else {
          this.recommentTopicList[index].praised = liked;
          this.recommentTopicList[index].praiseNum += liked === 1 ? 1 : -1;
        }
        if (data.data.addIntegral !== 0) {
          this.showMessage(e, '积分+' + data.data.addIntegral)
          return
        }
        if (liked === 1) {
          this.showMessage(e, '闪耀值+1')
        }
      }
    },
    // 关注话题
    async setTopicFollow(e, item, index) {
      let followType = item.followed === 1 ? 0 : 1
      let url = 'circle/customer/topic/setTopicFollow?topicId=' + item.id + '&followType=' + followType
      let {data} = await this.$javaCircle.doRequest("post", url, '', '', false)
      if (data.code !== 0) {
        uni.showToast({title: data.message, icon: "none"})
        return
      }
      if (data.data.addIntegral > 0) {
        this.showMessage(e, '积分+' + data.data.addIntegral)
      }
      this.recommentTopicList[index].followed = followType
    },
    end() {
      // if (this.recommentTopicPostData.page * this.recommentTopicPostData.pageSize < this.recommentTopicTotal) {
      this.recommentTopicPostData.page++
      this.getRecommentTopic()
      // }
    },
    showMessage(e, title) {
      this.$refs.toMessage.init(e.detail.x, e.detail.y, title)
    },
    toDetails(id, index, tag) {
      uni.navigateTo({url: "/pages/circle/details/details?id=" + id + '&index=' + index + '&tag=' + tag})
    },
    toClass(id, index) {
      uni.navigateTo({url: '/pages/circle/class/class?classId=' + id + '&index=' + index})
    },
    toRole() {
      uni.navigateTo({url: '/pages/circle/role?type=1'})
    },
    toSaveMessage(id) {
      uni.navigateTo({url: '/pages/circle/details/saveMessage?id=' + id})
    },
    toLog() {
      uni.navigateTo({url: '/pages/circle/log/log'})
    },
    setShare(item, index = 'null') {
      this.$emit('setTopicInfo', item)
      if (index === 'null') {
        this.todayTopic.circleTopicData.forwarding++
      } else {
        this.recommentTopicList[index].forwarding++
      }

    },
    // 上啦刷新
    onRefresh() {
      this.triggered = true
      this.recommentTopicPostData.page = 1
      this.getRecommentTopic()
    },
	onscroll(event) {
	  if(event.target.scrollTop >= this.systemInfo.windowHeight * 2 && !this.showBackTop) {
		  this.showBackTop = true;
	  } else if(event.target.scrollTop < this.systemInfo.windowHeight * 2 && this.showBackTop) {
		  this.showBackTop = false;
	  }  
	},
	backTop() {
		this.scrollTop = '';
		this.$nextTick(() => {
		  this.scrollTop = 0;
		})
	},
  },
}
</script>

<style lang="scss" scoped>
.button-item-active image {
  transform: scale(1);
}

.button-item-active:active image {
  transform: scale(2);
  transition: all .3s linear;
}

button {
  margin: 0;
  padding: 0;
  background-color: inherit;
  position: static;
}

button:after {
  content: none;
}

button::after {
  border: none;
}

.content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: auto;
  box-sizing: border-box;
}

.scroll-view {
  width: 100%;
  height: 72upx;
  white-space: nowrap;
  position: sticky;
  //top: 0px;
  z-index: 10;
}

.message-item {
  height: 100%;
  display: inline-block;
  background: linear-gradient(90deg, #A3FFC3 0%, #F4FFF7 100%);
  border-radius: 36upx;
  margin-right: 60upx;
  padding-right: 40upx;
}

.message {
  display: flex;
  align-items: center;
}

.user-img {
  width: 72upx;
  height: 72upx;
  border-radius: 50%;
  background-color: #666666;
  margin-right: 10upx;

  image {
    width: 100%;
    height: 100%;
    border-radius: 50%;
  }
}

.message-item .text {
  font-size: 32upx;
  color: #333;
  margin-right: 20upx;
}

.message-item .button {
  font-size: 32upx;
  color: #52A2FF;
  font-weight: 500;
  text-decoration: underline;
}

.class-box {
  width: 100%;
  box-sizing: border-box;
  padding: 30upx;
  background: linear-gradient(180deg, #FFFFFF 50%, #F3F5F8 100%);
}

.class-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 30upx;
}

.title-box {
  display: flex;
  align-items: center;
}

.class-title .class-icon {
  width: 41upx;
  height: 40upx;
  margin-right: 19upx;

  image {
    width: 100%;
    height: 100%;
  }
}

.class-msg {
  height: 40upx;
  position: relative;
  width: 400upx;

  .text1 {
    line-height: 40upx;
    position: absolute;
    font-size: 40upx;
    color: #333;
    font-weight: 600;
    z-index: 2;
    top: 0;
    left: 0;
  }

  .class-msg-xian {
    z-index: 1;
    position: absolute;
    left: 0;
    bottom: 0upx;
    width: 118upx;
    height: 8upx;
    background: linear-gradient(to right, #54B174 0%, #fff 100%);
  }
}

.class-title .msg {
  font-size: 28upx;
  color: #585858;
  text-decoration: underline;
}

.class-list {
  display: grid;
  column-count: 4;
  display: flex;
  grid-gap: 15upx;
  flex-wrap: wrap;
}

.class-item {
  width: 220upx;
  height: 220upx;
  border-radius: 10upx;
  position: relative;
  background-color: #D8D8D8;

  .image {
    width: 100%;
    height: 100%;
    border-radius: 10upx;

    image {
      width: 100%;
      height: 100%;
      border-radius: 10upx;
    }
  }

  .title {
    position: absolute;
    left: 0;
    bottom: 0;
    width: fit-content;
    box-sizing: border-box;
    padding: 0 16upx;
    color: #fff;
    font-size: 34upx;
    font-weight: 600;
    border-radius: 0 10upx 10upx 10upx;
    background-color: #54B174;
  }
}

.class-item-num4 {
  width: 162upx !important;
  height: 162upx !important;
}

.item {
  width: 100%;
  box-sizing: border-box;
  padding: 30upx;
  background-color: #fff;
}

.day-top {
  width: 100%;
  height: 110upx;
  border-radius: 20upx 20upx 0 0;
  position: relative;

  image {
    width: 100%;
    height: 100%;
  }
}

.toDay {
  font-size: 32upx;
  color: #fff;
  position: absolute;
  top: 50%;
  right: 20upx;
  transform: translate(0%, -50%);
  display: flex;
  align-items: center;
  height: 100upx;
  width: 159upx;
  justify-content: space-between;
}

.day-content {
  width: 100%;
  border-radius: 0 0 20upx 20upx;
  border: 1upx solid #979797;
  border-top: 0upx solid #979797;
}

.topic-box {
  box-sizing: border-box;
  padding: 20upx;
  padding-bottom: 0upx;
  position: relative;

  .topic-top {
    width: 100%;
    display: flex;

    .image {
      width: 140upx;
      height: 140upx;
      border-radius: 8upx;
      margin-right: 20upx;
      position: relative;

      image {
        width: 100%;
        height: 100%;
        border-radius: 8upx;
      }

      .image-icon {
        position: absolute;
        width: 75upx;
        height: 34upx;
        border-radius: 8upx;
        color: #fff;
        font-size: 26upx;
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: #F55845;
        right: 0;
        bottom: 0;
        font-weight: 600;
      }

      .tuijian {
        background-color: #F7C341;
      }
    }

    .topic-info {
      flex: 1;

      .title {
        font-size: 42upx;
        color: #000;
        font-weight: 500;
        margin-bottom: 24upx;
      }

      .tag {
        flex: 1;
        overflow: hidden;
        white-space: nowrap;
        margin-bottom: 8upx;

        text {
          background-color: #F1FAF3;
          color: #6AAC80;
          font-size: 30upx;
          box-sizing: border-box;
          padding: 0 8upx;
          border-radius: 8upx;
          margin-right: 12upx;
        }
      }

      .tagAfter::after {
        content: "...";
        display: inline-block;
        background-color: #F1FAF3;
        width: 0;
        color: #6AAC80;
      }

      .userCount {
        font-size: 28upx;
        color: #999999;
      }
    }
  }

  .source-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .source {
    width: 400upx;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-size: 28upx;
    color: #999999;
  }


  .button {
    height: 68upx;
    box-sizing: border-box;
    padding: 0 10upx;
    border-radius: 39upx;
    background-color: #54B174;
    color: #fff;
    line-height: 68upx;
    text-align: center;
    font-size: 36upx;
    font-weight: 500;
  }

  .yi-button {
    background-color: #F2F2F2 !important;
    color: #C8C8C8 !important;
  }

  .desc {
    //width: 600upx;
    font-size: 38upx;
    color: #666666;
    margin-top: 18upx;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .buttons {
    width: 100%;
    border-top: 1upx solid #D8D8D8;
    height: 98upx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 31upx;

    .button-item {
      width: 30%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 26upx;
      color: #666;

      image {
        width: 44upx;
        height: 44upx;
        margin-right: 4upx;
      }
    }
  }
}

.top-list {
  padding: 0upx !important;

  .button {
    border: none !important;
    right: 30upx !important;
  }

  .class-title {
    padding: 30upx;
    padding-bottom: 0 !important;
    margin-bottom: 0 !important;
  }

  .desc {

  }

  .topic-box {
    padding: 30upx !important;
    padding-bottom: 0 !important;
  }

  .class-msg-xian {
    background: linear-gradient(to right, #FD9A00 0%, #fff 100%);
  }

  .list {
    background-color: #F3F5F8;
    padding-bottom: 200upx;
  }

  .topic-item {
    margin-bottom: 24upx;
    background-color: #fff;
  }

  .button {
    width: 122upx;
    height: 54upx;
    line-height: 54upx !important;
    text-align: center;
    background-color: #C9EFD6;
    border: 1upx solid #54B174;
    color: #54B174;
    font-size: 28upx;
  }
}

.user-comment-box {
  width: 100%;
  height: 120upx;
  border-radius: 12upx;
  background-color: #F1F1F1;
  margin-top: 15upx;
  position: relative;
  box-sizing: border-box;
  padding: 16upx 18upx;

  .user-info {
    width: 100%;
    height: 100%;
    display: flex;

    .user-image {
      width: 88upx;
      height: 88upx;
      background-color: #666666;
      border-radius: 50%;
      margin-right: 12upx;

      image {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }

    .user-comment {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .username {
        width: 510upx;
        display: flex;
        align-items: center;

        .name {
          font-size: 28upx;
          color: #666666;
        }

      }

      .comment {
        width: 500upx;
      }
    }
  }

  .icon-box {
    width: 60upx;
    height: 28upx;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;

    image {
      width: 100%;
      height: 100%;
    }
  }
}

.zhanwei {
  width: 1upx;
  height: 100%;
  margin-left: 0;
  transition: all .3s linear;
}

.images {
  display: flex;
  flex-wrap: wrap;
  column-count: 3;
  column-gap: 25upx;
  margin-top: 30upx;

  .image-item {
    width: 174upx;
    height: 174upx;
    margin-bottom: 25upx;
    position: relative;

    .image {
      width: 100%;
      height: 100%;
    }

    .video-play {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      background-color: rgba(0, 0, 0, .7);
      display: flex;
      align-items: center;
      justify-content: center;

      image {
        width: 62upx;
        height: 62upx;
      }
    }
  }
}

.tag-box {
  display: flex;
  align-items: center;
  justify-content: space-between;

  .button {
    margin-left: auto;
  }
}
</style>
